﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Exhibition.Dao.ViewModel;

using Exhibition.Common.Extension;
using Exhibition.Service;
using Exhibition.Common.Helper;
namespace Exhibition.Handler
{
    /// <summary>
    /// MenuHandler 的摘要说明
    /// </summary>
    public class MenuHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string pid = string.Empty ;
            int curid = 0;

            if (!string.IsNullOrEmpty(context.Request["id"]))
            {
                pid = context.Request["id"];
            }
            if (!string.IsNullOrEmpty(context.Request["CurId"]))
            {
                curid = Convert.ToInt32(context.Request["CurId"]);
            }


            List<TreeModel> treeModels = BuildTree(pid,  curid);
            context.Response.ContentType = "text/json;charset=UTF-8;";
            context.Response.Write(treeModels.ToJsonItem().Replace("IsChecked", "checked"));
        }


        public List<TreeModel> BuildTree(string pid, int curid)
        {
            //List<FuncModel> menus = CenterService.funcService.GetListByPid(pid);
            List<FuncModel> menus = CenterService.funcService.GetListByFunCode(pid);
            RoleModel rmdl=   CenterService.roleService.GetOneById(curid);

            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus)
            { 

                if (MembershipService.GetUser() != null)
                {
                    bool flg=false;
                    List<FuncModel> sonMdl = CenterService.funcService.GetListByFunCode(menu.FuncCode);
                   
                    TreeModel treeModel = new TreeModel();
                    treeModel.funcode = menu.FuncCode;
                    treeModel.id = menu.FuncCode;
                    if (rmdl != null)
                    {
                        if (rmdl.Funcs.IndexOf(menu.FuncCode.ToString()) >= 0)
                        {
                            treeModel.IsChecked = true;
                        }
                        //判断子类是否有选中的 如果选中 则父类需要选中
                        foreach (var smd in sonMdl) {
                            if (rmdl.Funcs.IndexOf(smd.FuncCode.ToString()) >= 0)
                            {
                                flg = true;
                            }
                        }
                        if (flg)
                        {
                            treeModel.IsChecked = true;
                        }
                    }

                    treeModel.state = sonMdl.Count > 0 ? "closed" : "open";
                    treeModel.text = menu.FuncName;
                    treeModel.tagurl = menu.FuncPath;
                    treeModels.Add(treeModel); 

                }
            


            }
            return treeModels;


        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}